/**
 * 有待研究
 * @author honway
 * @date 2019/4/18 16:43.
 */
public interface Context {
    State state();

    void state(State state);
}

interface State{
    boolean process(Context context);
}

enum States implements State{
    RED {
        public boolean process(Context context) {
            context.state(States.GREEN);
            System.out.println("Current State = " + this);
            return true;
        }
    }, GREEN  {
        public boolean process(Context context) {
            context.state(States.BLACK);
            System.out.println("Current State = " + this);
            return true;
        }
    }, BLACK{
        @Override
        public boolean process(Context context) {
            context.state(States.YELLOW);
            System.out.println("Current State = " + this);
            return true;
        }
    },YELLOW{
        @Override
        public boolean process(Context context) {
            context.state(States.WHITE);
            System.out.println("Current State = " + this);
            return true;
        }
    }, WHITE{
        @Override
        public boolean process(Context context) {
            context.state(States.BLUE);
            System.out.println("Current State = " + this);
            return true;
        }
    }, BLUE{
        @Override
        public boolean process(Context context) {
            context.state(States.RED);
            System.out.println("Current State = " + this);
            return false;
        }
    };
    public abstract boolean process(Context context);
}

class ContextImp implements Context{

    State state;

    @Override
    public State state() {
        return state;
    }

    @Override
    public void state(State state) {
        this.state = state;
    }
}

class InfiniteMachineTest {
    public static void main(String[] args) {
        Context context = new ContextImp();
        context.state(States.WHITE);
        while (context.state().process(context));
    }
}